import { useState } from 'react';

import { useRequest } from '@szzj/hooks';

import { getUserDetail } from '@/services/permission/user';

import useAppCode from './useAppCode';

import type { IUserItem } from '@/types/permission/user';

export default () => {
  const { appCode, userAppCode } = useAppCode();

  const [open, setOpen] = useState(false);
  const detailReq = useRequest(getUserDetail, {
    manual: true,
  });

  function onOpen(data: IUserItem) {
    if (data.id) {
      detailReq.run({
        id: data.id,
        appCode,
        userAppCode,
      });
    }

    setOpen(true);
  }

  function onClose() {
    detailReq.mutate();
    setOpen(false);
  }

  return {
    open,
    detailReq,
    onOpen,
    onClose,
  };
};
